package cn.hhchat.kit.file;

import cn.hhchat.kit.exception.DirCreateFailedException;
import cn.hhchat.kit.uuid.UUIDUtils;

import java.io.File;
import java.io.InputStream;

/**
 * Created this one by HMH on 2017/5/22.
 */
public class FileUtil {

    public static File createFileWithUUID(String rootPath, String fileName) throws Exception {
        String preStr = UUIDUtils.getUUIDWithTime();
        //生成文件名
        if(rootPath.endsWith("/")) rootPath = rootPath.substring(0, rootPath.length() - 1);
        String filename = rootPath + "/" + preStr + fileName;
        //去除空格并生成文件
        File fileToSave = new File(filename.replaceAll("%20", " "));
        //查看文件夹目录情况
        File dic = new File(fileToSave.getParent());
        if (!dic.exists()) {
            //不存在则递归新建
            if(dic.mkdirs()){
                throw new DirCreateFailedException("无法创建文件夹");
            }
        }
        return fileToSave;
    }
}
